//
//  UIViewExt.swift
//  ChingoScheduleItem
//
//  Created by justtcf on 2017/7/19.
//  Copyright © 2017年 HJB. All rights reserved.
//

import UIKit

// MARK: 控制链拓展（UIViewController）
extension UIView {
    
    var viewController: UIViewController? {
        // 没有更上层的响应者了
        if self.next == nil {
            return nil
        }
        
        // 循环遍历查找是否有VC，有则返回
        var nextResponse: UIResponder? = self.next
        while nextResponse != nil {
            if nextResponse is UIViewController {
                return nextResponse as? UIViewController
            } else {
                nextResponse = nextResponse?.next
            }
        }
        
        // 如果遍历结束还没找到就返回nil
        return nil
    }
}

// MARK: 视图位置拓展
extension UIView {
    
    /// 宽度
    var width: CGFloat {
        get { return self.frame.size.width }
        set { self.frame.size.width = newValue }
    }
    
    /// 高度
    var height: CGFloat {
        get { return self.frame.size.height }
        set { self.frame.size.height = newValue }
    }
    
    /// 左侧点
    var left: CGFloat {
        get { return self.frame.origin.x }
        set { self.frame.origin.x = newValue }
    }
    /// 右侧点-视图距离父视图右侧的距离
    var right: CGFloat {
        get { return left + width }
        set { self.frame.origin.x = newValue - width }
    }
    /// 上侧点
    var top: CGFloat {
        get { return self.frame.origin.y }
        set { self.frame.origin.y = newValue }
    }
    /// 下侧点
    var bottom: CGFloat {
        get { return top + height }
        set { self.frame.origin.y = newValue - height }
    }
    
    /// 起点
    var origin: CGPoint {
        get { return self.frame.origin }
        set { self.frame.origin = newValue }
    }
    
    /// x中点
    var x: CGFloat {
        get { return self.center.x }
        set { self.center.x = newValue }
    }
    
    /// y中点
    var y: CGFloat {
        get { return self.center.y }
        set { self.center.y = newValue }
    }
    
    
}
